import { setCommand, type ApplianceCommand } from '@/api/appliance'
import {
  BSH_COMMON_COMMAND_APPLYFACTORYRESET,
  BSH_COMMON_COMMAND_APPLYNETWORKRESET,
  BSH_COMMON_COMMAND_LEARNINGDISHWASHER_RESET,
} from '@/constants/Command'
import { showLoading, toHome, toPage } from '@/utils/mpHelper'
import i18n from '@/i18n'
import { setLearningUsedCache } from '@/controller/Dishwasher/Learning/cache'
import { TrackType, trackEvent } from '@/dataReport/tracks'

export type CommandsGroup = {
  commandsGroup: string
  commandName: string
  visible: boolean
  commands: CommandItem[]
}
export type CommandItem = {
  key: string
  name: string
  btnName: string
  confirmTitle: string
  confirmTips: string
  des: string
}

type RefValue<T> = Ref<T> | ComputedRef<T>
export function useCommandsReset(
  commands: Ref<ApplianceCommand[]>,
  options: { guest: RefValue<boolean>; isVirtual: RefValue<boolean> }
) {
  const { guest, isVirtual } = options
  const isSupportCommand = (key: string) => commands.value.some((command) => command.key === key)

  const commandsGroups = computed<CommandsGroup[]>(() => [
    {
      commandsGroup: 'CommandsGroup.Programs',
      commandName: 'Appliance.Settings.Group.SmartProgram.Label',
      visible: isSupportCommand(BSH_COMMON_COMMAND_LEARNINGDISHWASHER_RESET),
      commands: [
        {
          key: BSH_COMMON_COMMAND_LEARNINGDISHWASHER_RESET,
          name: 'Appliance.Settings.Group.SmartProgram.Label',
          btnName: 'Appliance.Settings.SmartProgram.Title',
          confirmTitle: 'Appliance.Settings.Group.Reset.Button.Text',
          confirmTips: 'Appliance.Settings.SmartProgram.Confirm.Tips',
          des: '重置AI智洗程序至初始状态',
        },
      ],
    },
    {
      commandsGroup: 'SettingsGroup.Network',
      commandName: 'Appliance.Settings.ResetNetwork.Title',
      visible: !guest.value && isSupportCommand(BSH_COMMON_COMMAND_APPLYNETWORKRESET),
      commands: [
        {
          key: BSH_COMMON_COMMAND_APPLYNETWORKRESET,
          name: 'Appliance.Settings.ResetNetwork.Label',
          btnName: 'Appliance.Settings.Group.Reset.Button.Text',
          confirmTitle: 'Appliance.Settings.ResetNetwork.Label',
          confirmTips: 'Appliance.Settings.ResetNetwork.Content',
          des: '',
        },
      ],
    },
    {
      commandsGroup: 'SettingsGroup.NonSettings',
      commandName: 'Appliance.Settings.Group.FactoryReset.Label',
      visible: !guest.value && isSupportCommand(BSH_COMMON_COMMAND_APPLYFACTORYRESET),
      commands: [
        {
          key: BSH_COMMON_COMMAND_APPLYFACTORYRESET,
          name: 'Appliance.Settings.ResetFactory.Label',
          btnName: 'Appliance.Settings.Group.Reset.Button.Text',
          confirmTitle: 'Appliance.Settings.ResetFactory.Title',
          confirmTips: 'Appliance.Settings.ResetFactory.Content',
          des: '',
        },
      ],
    },
  ])

  /**
   * 下发commands指令
   * @param haId
   * @param command
   */
  function resetCommand(haId: string, command: CommandItem) {
    uni.showModal({
      title: i18n.global.t(command.confirmTitle),
      content: i18n.global.t(command.confirmTips),
      success: ({ confirm }) => {
        confirm &&
          showLoading(async () => {
            // 重置AI智洗缓存
            if (command.key === BSH_COMMON_COMMAND_LEARNINGDISHWASHER_RESET) {
              setLearningUsedCache(haId, false)
            }

            /**
             * 虚拟家电
             */
            if (isVirtual.value) {
              _showResetSuccessToast()
              return toHome({ isVirtual: true })
            }

            /**
             * 真实家电
             */
            await setCommand(haId, command.key, true)
            _showResetSuccessToast()

            return toHome()
          })
        trackEvent(command.key, {}, TrackType.ApplianceDetail)
      },
    })
  }

  function _showResetSuccessToast() {
    uni.showToast({
      title: i18n.global.t('APP.Common.SetSucess.Text'),
    })
  }

  return {
    commandsGroups,
    resetCommand,
  }
}
